/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exceptioncourse;
// Note: This class won't compile by design!
import java.io.*;
import java.nio.charset.*;
import java.nio.file.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ListOfNumbers {

    public Vector vector;
    private List<Integer> list;
    private static final int SIZE = 10;

    public ListOfNumbers() {
        list = new ArrayList<Integer>(SIZE);
        for (int i = 0; i < SIZE; i++) {
            list.add(new Integer(i));
        }
        vector = new Vector();
    }

    public void readList(String path) {
//        BufferedReader bf = null;
//        try (BufferedReader bf = new BufferedReader(new FileReader(path))){
//            //read all int value from file and add to vector
//            String str;
//            while ((str = bf.readLine()) != null ){
//                Integer i = Integer.parseInt(str);
//                vector.add(i);
//            }
        String line = null;
        try {
            RandomAccessFile raf = new RandomAccessFile(path, "r");
            while ((line = raf.readLine()) != null) {
                Integer i = new Integer(Integer.parseInt(line));
//                System.out.println(i);
                vector.addElement(i);
            }
        } catch (FileNotFoundException ex) {
            System.err.println("File "+ path +" not found");
        } catch (IOException io){
            System.err.println("IO Exception");
        } catch (NumberFormatException nfe){
            System.err.println("Number Format Exception");
        }
    }

    public static void writeToFileZipFileContens(
            String zipFileName, String outputFileName) throws IOException {
        Charset charset = StandardCharsets.US_ASCII;
        Path outputFilePath = Paths.get(outputFileName);

//        open zip file and create output file with
//        try-with-resource statement
        try (
                ZipFile zf = new ZipFile(zipFileName);
                BufferedWriter bWriter = Files.newBufferedWriter(outputFilePath, charset);) {
//            Enumerate each entry
            for (Enumeration entries = zf.entries();
                    entries.hasMoreElements();) {
//                Get the entry name and write it the output file
                String newLine = System.getProperty("line.separator");
                String zipEntryName =
                        ((ZipEntry) entries.nextElement()).getName() + newLine;
                bWriter.write(zipEntryName, 0, zipEntryName.length());
            }
        }
    }

    public void writeList() {
        PrintWriter out = null;

        try {
            System.out.println("Entering" + " try statement");

            out = new PrintWriter(new FileWriter("OutFile.txt"));
            for (int i = 0; i < SIZE; i++) {
                out.println("Value at: " + i + " = " + vector.elementAt(i));
            }

        } catch (NullPointerException e){
            System.err.println("Caught NullPointerException: " + e.getMessage());
        } catch (ArrayIndexOutOfBoundsException e) {
            System.err.println("Caught ArrayIndexOutOfBoundsException: "
                    + e.getMessage());

        } catch (IOException e) {
            System.err.println("Caught IOException: " + e.getMessage());

        } finally {
            if (out != null) {
                System.out.println("Closing PrintWriter");
                out.close();
            } else {
                System.out.println("PrintWriter not open");
            }
        }
    }
}